package com.leilei.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {

    public BaseServlet(){

    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取调用的Servlet的Class对象
        Class<? extends BaseServlet> cs = this.getClass();

        //获取连接中传递的method参数的值
        String method = req.getParameter("method");

        try {
            //获取对象中method参数值对应的方法
            Method m = cs.getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            //设置方法可被调用
            m.setAccessible(true);
            //执行方法
            m.invoke(this, req, resp);

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

}
